package panda.client.ui.widgets;

import panda.client.enumerations.Color;
import panda.client.enumerations.ZIndexLevel;
import panda.client.listeners.CounterDragListener;
import panda.client.listeners.TooltipListener;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Label;

/**
 * A label with a background image representing a draggable counter.  Can't be a straight gwt.Image because
 * of firefox's image and text dragging 'feature' D:
 * 
 * @author zane.tian@gmail.com
 */
public class Counter extends Label implements InPlay {
	private Color color;
	private String imgUrl;
	private long serverID;
	private boolean dragging = false;

	public static final int width = 19;
	public static final int height = 19;

	public Counter(Color color, long serverID) {
		super();
		this.color = color;
		this.serverID = serverID;

		switch(color) {
			case BLUE:
				imgUrl = "url(counters/blue-shade.gif)";
				break;
			case RED:
				imgUrl = "url(counters/red-shade.gif)";
				break;
			case GREEN:
				imgUrl = "url(counters/green-shade.gif)";
				break;
			default:
				throw new IllegalArgumentException("Color " + color + " not supported in making a Counter");
		}

		this.addMouseListener(new CounterDragListener());
		this.addMouseListener(new TooltipListener());

		DOM.setStyleAttribute(getElement(), "opacity", "0.7");
		DOM.setStyleAttribute(getElement(), "filter", "alpha(opacity=70)");

		/* wtf: setting to background-image causes transparency to fail but this works in ie6+7+safari+firefox... */
		DOM.setStyleAttribute(getElement(), "background", imgUrl);
		DOM.setStyleAttribute(getElement(), "zIndex", String.valueOf(ZIndexLevel.COUNTER.getZIndex()));
		DOM.setStyleAttribute(getElement(), "width", width + "px");
		DOM.setStyleAttribute(getElement(), "height", height + "px");
	}
	
	public long getServerID() {
		return serverID;
	}
	
	public Color getColor() {
		return color;
	}

	public String getName() {
		return color.getName() + " Counter [" + serverID + "]";
	}

	public boolean isDragging() {
		return dragging;
	}
	
	public void setDragging(boolean dragState) {
		dragging = dragState;
	}
}